﻿using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrossSelling.Core.ServiceCommand.Admin
{
    public class AddProductChecklistInfoServiceCommand : ProviderServiceCommand<IProviderFactory, AddProductInfoRequest, ResponseRecord>
    {
        private UserRecord Dao;
        private int ProductID;
        public AddProductChecklistInfoServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(AddProductInfoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.Record == null)
            {
                throw new ArgumentException("Record must be set", "request");
            }
            else
            {
                ProductID = request.Record.ProductId;
            }
            if (request.CurrentDao == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentDao;
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            try
            {
                bool isUpdate = true;
                ProductHandbookProduct checkProductInfo = Providers.ProductHandbooks.RetrieveProductByProductId(Request.Record.ProductId);
                if (checkProductInfo != null)
                {
                    if (checkProductInfo.ProductStatus == 2)
                    {
                        isUpdate = false;
                        response.ErrorMessage = "Product info is existed";
                    }
                    else
                    {
                        //ProductHandbookProductDetail productDetail = new ProductHandbookProductDetail
                        //{
                        //    ProductId = ProductID,
                        //    ProductName = checkProductInfo.ProductName,
                        //    ProductNameEn = checkProductInfo.ProductNameEn,
                        //    ConditionProduct = string.Empty,
                        //    ConditionProductEn = string.Empty,
                        //    DocumentProduct = string.Empty,
                        //    DocumentProductEn = string.Empty,
                        //    XSellingProduct = string.Empty,
                        //    XSellingProductEn = string.Empty,
                        //    OverviewProduct = string.Empty,
                        //    OverviewProductEn = string.Empty
                        //};

                        List<ChecklistSubProduct> checklistSubProduct = new List<ChecklistSubProduct>();
                        checklistSubProduct.Add(new ChecklistSubProduct
                        {
                            ProductId = ProductID,
                            SubProductName = checkProductInfo.ProductName + " A1",
                            SubProductNameEn = checkProductInfo.ProductNameEn + " A1"
                        });
                        checklistSubProduct.Add(new ChecklistSubProduct
                        {
                            ProductId = ProductID,
                            SubProductName = checkProductInfo.ProductName + " A2",
                            SubProductNameEn = checkProductInfo.ProductNameEn + " A2"
                        });

                        checkProductInfo.ProductStatus = 2;
                        isUpdate = Providers.ProductHandbooks.AddChecklistSubProduct(checklistSubProduct) && Providers.ProductHandbooks.SaveProductHandbookProductByProductID(checkProductInfo);
                    }
                }
                else
                {
                    isUpdate = false;
                    response.ErrorMessage = "Product not exists";
                }
                return isUpdate;
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message);
            }
        }
    }
}
